﻿using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace StockholmsStad.Utilities
{
    internal static class WebConfigWriter
    {
        private const string APP_CONFIG = "app.config";
        private const string WEB_CONFIG = "web.config";

        internal static void GenerateFile(string outputFileDir)
        {
            var appConfigFile = Path.Combine(outputFileDir, APP_CONFIG);
            var webConfigFile = Path.Combine(outputFileDir, WEB_CONFIG);

            if (File.Exists(appConfigFile))
            {
                var xDoc = XDocument.Load(appConfigFile);

                // Remove host from config file since this is unnecessary information
                xDoc.Descendants().Where(e => e.Name.LocalName == "host").Remove();

                xDoc.Save(webConfigFile);
            }
            else
            {
                Program.WriteWarning(string.Format("Could not find file: {0}", appConfigFile));
            }
        }
    }
}
